*This file is designed to take the married couple and single person Census files and produce a sample dataset of natives for analysis
clear
set mem 6g
set more off
global path "u:\user3\klp27\Blau and Kahn\Immigration"

*Select natives and append
foreach year of numlist 1980 1990 2000 {
  use "$path\Census `year' Wife data", clear
  gen immigrant=.
  replace immigrant=0 if citizen==1 | bpld<15000 | bpld==71033 | (bpld>=71040 & bpld<=71049)
  replace immigrant=1 if citizen~=1 & bpld>=15000 & bpld~=71033 & (bpld<71040 | bpld>71049)
  gen spimmigrant=.
  replace spimmigrant=0 if spcitizen==1 | spbpld<15000 | spbpld==71033 | (spbpld>=71040 & spbpld<=71049)
  replace spimmigrant=1 if spcitizen~=1 & spbpld>=15000 & spbpld~=71033 & (spbpld<71040 | spbpld>71049)
  keep if immigrant==0 & spimmigrant==0
  save "$path\Census `year' Native Wife data", replace

  use "$path\Census `year' Single Women data", clear
  gen immigrant=.
  replace immigrant=0 if citizen==1 | bpld<15000 | bpld==71033 | (bpld>=71040 & bpld<=71049)
  replace immigrant=1 if citizen~=1 & bpld>=15000 & bpld~=71033 & (bpld<71040 | bpld>71049)
  keep if immigrant==0
  save "$path\Census `year' Native Single Women data", replace

  use "$path\Census `year' Single Men data", clear
  scalar year=`year'
  if scalar(year)==1980 for var pernum slwt perwt momloc momrule poploc poprule sploc sprule relate age sex race marst nchild bpl citizen yrimmig yrsusa2 hispan school higraded educrec schltype empstat labforce occ ind wkswork1 uhrswork inctot incwage incinvst migplac5 migmet5 migtype5 migcity5 migcogrp movedin incbus incfarm classwkd qwkswork quhrswor qincbus qincfarm qincinvs qincwage qbpl qyrimm speakeng occmo: rename X spX
  if scalar(year)==1990 for var pernum slwt perwt momloc momrule poploc poprule sploc sprule relate age sex race marst nchild bpl citizen yrimmig yrsusa2 hispan school educrec educ99 schltype empstat labforce occ ind wkswork1 uhrswork inctot incwage incinvst migplac5 migmet5 migtype5 migcity5 migpuma migpumas movedin incbus incfarm classwkd qwkswork quhrswor qincbus qincfarm qincinvs qincwage qbpl qyrimm speakeng occmo: rename X spX
  if scalar(year)==2000 for var pernum slwt perwt momloc momrule poploc poprule sploc sprule relate age sex race marst nchild /*racgen00 racdet00*/ racamind racasian racblk racpacis racwht racother racnum bpl citizen yrimmig yrsusa1 yrsusa2 hispan school educrec educ99 gradeatt schltype empstat labforce occ ind wkswork1 uhrswork inctot incwage incinvst migplac5 migmet5 migtype5 migtyp00 migcity5 migpuma migpumas movedin incbus00 classwkd qwkswork quhrswor qincbus qincinvs qincwage qbpl qyrimm speakeng occmo: rename X spX
  gen spimmigrant=.
  replace spimmigrant=0 if spcitizen==1 | spbpld<15000 | spbpld==71033 | (spbpld>=71040 & spbpld<=71049)
  replace spimmigrant=1 if spcitizen~=1 & spbpld>=15000 & spbpld~=71033 & (spbpld<71040 | spbpld>71049)
  keep if spimmigrant==0
  save "$path\Census `year' Native Single Men data", replace

  use "$path\Census `year' Native Wife data", clear
  append using "$path\Census `year' Native Single Women data"
  append using "$path\Census `year' Native Single Men data"

  gen hispnwhite=0
  replace hispnwhite=1 if (hispan>0 & hispan~=.) | (sphispan>0 & sphispan~=.)
  replace hispnwhite=1 if (raced>=200 & raced~=.) | (spraced>=200 & spraced~=.)
  gen sample=0
  replace sample=1 if subsamp==0 | subsamp==10 | subsamp==20 | subsamp==30

  preserve
  keep if hispnwhite==1
  save "$path\Census `year' Minority Native data", replace

  restore
  *THINK ABOUT NEXT LINES
  replace perwt=25*perwt if sample==1
  replace spperwt=25*spperwt if sample==1
  replace hhwt=25*hhwt if sample==1

  keep if sample==1

  save "$path\Census `year' Native data", replace
  erase "$path\Census `year' Native Wife data.dta"
  erase "$path\Census `year' Native Single Women data.dta"
  erase "$path\Census `year' Native Single Men data.dta"
}
